package com.videomaru
{
	import flash.text.TextField;
	/**
	 * General purpose logging/debug class. This class is implented as a static class so that any class can use a direct reference or wrap around it for custom output.
	 */
	 
	public class Log 
	{
		public  static var lastMessage:String;
		public  static var outputSimilarMessages:Boolean = true;
		public  static var maxMessageLength:Number = 0;
		public  static var maxChars:Number = 20000;
		public  static var debugTextField:TextField;
		
		public static var FUNCTION:String = "function";
		public static var ERROR:String = "error";
		public static var INFO:String = "info";
		
		/*******************************************************
		* By default, the log does not output anything
		*******************************************************/
		private static var _outputLevel:Number = 0;
		
		/*******************************************************
		* Set what kind of info should be outputted from the log
		* 
		* 0 = no output (default)
		* 1 = only errors
		* 2 = errors and method calls
		* 9 = all output
		*******************************************************/ 
		public static function set outputLevel(l:Number):void
		{
			_outputLevel = l;
		}
		public static function get outputLevel():Number
		{
			return _outputLevel;
		}
		
		/*******************************************************
		* Redirect all output to a textfield instead of the
		* output panel.
		* 
		* @param t An instance of the TextField class
		*******************************************************/
		public static function outputTo(t:TextField):void
		{
			debugTextField = t;
		}
		
		/*******************************************************
		* This is the function you'll call from your code
		* 
		* @param msg A string with debug text to show
		*******************************************************/
		public static function dbg(msg:String, type:String = ""):void
		{
			if(outputLevel == 1 && type == ERROR){
				output(msg);
			} else if(outputLevel == 2 && (type == ERROR || type == FUNCTION) ){
				output(msg);
			} else if(outputLevel == 9){
				output(msg);
			}
		}
		private static function output(msg:String):void{
			if(msg == lastMessage && !outputSimilarMessages){
				// No Output
			} else {
				// Limit length of line (if required)
				if(maxMessageLength != 0){
					if(msg.length > maxMessageLength){
						msg = msg.substr(0,maxMessageLength);
					}
				}
				
				// Build the Log message
				var msg:String = msg;
				
				// Output the Log message to output panel, but only if there is no texfield set
				if(debugTextField != null){
					// Chop the max length of the textfield
					var oldTxt:String = debugTextField.text;
					if(oldTxt.length > maxChars){
						oldTxt = oldTxt.substr(oldTxt.length-maxChars,maxChars);
						debugTextField.text = oldTxt;
					} else {
						debugTextField.appendText(msg+"\n");
					}
					debugTextField.scrollV = debugTextField.maxScrollV;
				} else {
					trace(msg);
				}
				
				// Save the old message for comparison
				lastMessage = msg;
			}
		}
	}
}